<template>
	<view>
		<u-modal v-model="show" title="绑定平台账号" @confirm="submit()" ref="uModal" @cancel="handleCancel" :show-cancel-button	="true" :async-close="true">
			<view class="slot-content" style="padding: 0 40rpx;">
				<u-form :model="form" ref="uForm" label-width="120rpx">
					<u-form-item label="手机号" prop="mobile">
						<u-input v-model="form.mobile" placeholder="请输入手机号" />
					</u-form-item>
					<u-form-item label="账号" prop="usercode">
						<u-input v-model="form.usercode" placeholder="请输入账号" />
					</u-form-item>
					<u-form-item label="密码" prop="pwd">
						<u-input v-model="form.pwd" placeholder="请输入密码" />
					</u-form-item>
					<u-form-item label="验证码" prop="code" class="code">
						<view class="box box-align-center box-pack-between" style="width: 100%;">
							<u-input v-model="form.code" placeholder="请输入验证码" />
							<view class="forget-password">
								<view class="wrap">
									<u-toast ref="uToast"></u-toast>
									<u-verification-code :seconds="seconds" ref="uCode" @change="codeChange">
									</u-verification-code>
									<u-button size="mini" @click="getCode">{{tips}}</u-button>
								</view>
							</view>
						</view>
					</u-form-item>
				</u-form>
			</view>
		</u-modal>
	</view>
</template>

<script>
	export default {
		props: ['show'],
		data() {
			return {
				tips: '',
				// refCode: null,
				seconds: 60,
				form: {
					mobile: '',
					code: '',
					openid:null,
					usercode:'',
					pwd:''
				},
				authCode:'',
				rules: {
					mobile: [{
							required: true,
							message: '请输入手机号',
							// 可以单个或者同时写两个触发验证方式 
							trigger: ['change', 'blur'],
						},
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// this.$u.test.mobile()就是返回true或者false的
								return this.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							// 触发器可以同时用blur和change
							trigger: ['change', 'blur'],
						}
					],
					code: [{
						required: true,
						message: '请输入验证码',
						trigger: ['change', 'blur'],
					}],
				},
				user:uni.getStorageSync("user"),
			}
		},
		mounted() {
			console.log(this.show);
			this.getUnionidOnly()
		},
		methods: {
			getUnionidOnly() {
				let _this = this;
				wx.login({
					success: res => {
						_this.$api.post({
							url: '/comm/getAppletUnionid'
						}, {
							code: res.code
						}).then(s => {
							_this.form.openid = s.openid
							uni.setStorageSync("openid", s.openid)
						})
					}
				})
			},
			handleCancel(){
				this.$emit('handleClose',false)
			},
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if (this.form.mobile == "") return this.$u.toast('手机号不能为空');
				if (!this.$u.test.mobile(this.form.mobile)) return this.$u.toast('手机号不正确');
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					this.form.code = this.createCode()
					this.authcode = this.form.code
					return
					setTimeout(() => {
						uni.hideLoading();
						this.authcode = this.createCode()
						this.$api.post({
							url: '/comm/sendMsg'
						}, {
							mobile: this.form.mobile,
							authCode: this.authcode
						}).then(res => {
							//console.log('><>>' + res)
							if (res == "success") {
								this.$u.toast('验证码已发送');
							} else {
								uni.showToast({
									title: '验证码发送异常，请稍后再试',
									icon: 'none'
								})
							}
						})
						
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			},
			submit() {
				console.log(this.form.code,this.authcode);
				if(this.form.code != this.authcode){
					this.$u.toast('验证码不正确')
					this.$refs.uModal.clearLoading();
					return
				}else{
					this.$refs.uForm.validate(valid => {
						if (valid) {
							let opts = {
								url: '/bindPlatform',
								method: 'post'
							};
							this.$api.post(opts, this.form).then(res => {
								if (res.code == 0) {
									uni.showToast({
										title: res.msg,
										icon: 'none'
									})
									
									this.BindShow = false
									this.$refs.uModal.clearLoading();
									uni.setStorageSync("user",res.user)
									uni.setStorageSync("currSchool",res.school)
									uni.setStorageSync("roles", res.roles);
									uni.setStorageSync('role', res.roles.filter(o=>o!=`jyzz`)[0]);
									uni.reLaunch({
										url:"/pages/index/index"
									})
								}else if(res.code == 10002){
									uni.showToast({
										title: res.msg,
										icon: 'none'
									})
								}else if(res.code == 10003){
									uni.showToast({
										title: res.msg,
										icon: 'none'
									})
								}else{
									uni.showToast({
										title: res.msg,
										icon: 'none'
									})
								}
							
							})
						} else {
							console.log('验证失败');
							this.$refs.uModal.clearLoading();
						}
					});
				}
				
			},
			//验证码
			createCode() { //生成6位随机数
				var code = '';
				var codeLength = 6;
				var random = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
				for (var i = 0; i < codeLength; i++) {
					var index = Math.floor(Math.random() * 9);
					code += random[index];
				}
				return code;
			}			
		},
		// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		}
	}
</script>

<style scoped>
	/deep/ .u-form-item {
		position: relative;
		border-bottom: 1px solid #EBEFF8;
	}

	.forget-password {
		/* position: absolute;
		top: 90rpx;
		right: 0; */
		z-index: 1000000;
	}
</style>
